IF(NOT FFMPEG_LIBAVFORMAT_FOUND) 
	SET(FFMPEG_INCLUDE_SEARCH_DIRS 
		${FFMPEG_ROOT}/include 
		$ENV{FFMPEG_DIR}/include
		~/Library/Frameworks 
		/Library/Frameworks 
		/usr/local/include 
		/usr/include/ 
		/sw/include # Fink 
		/opt/local/include # DarwinPorts 
		/opt/csw/include # Blastwave 
		/opt/include 
		/usr/freeware/include
		${CMAKE_SOURCE_DIR}/extern/ffmpeg/include)

	SET(FFMPEG_LIBRARY_SEARCH_DIRS
		${FFMPEG_ROOT}/lib 
		$ENV{FFMPEG_DIR}/lib 
		~/Library/Frameworks 
		/Library/Frameworks 
		/usr/local/lib 
		/usr/local/lib64 
		/usr/lib 
		/usr/lib64 
		/sw/lib 
		/opt/local/lib 
		/opt/csw/lib 
		/opt/lib 
		/usr/freeware/lib64
		${CMAKE_SOURCE_DIR}/extern/ffmpeg/lib)

	MACRO(FFMPEG_FIND varname shortname headername) 
		FIND_PATH(FFMPEG_${varname}_INCLUDE_DIRS ${headername} ${FFMPEG_INCLUDE_SEARCH_DIRS}) 

		IF(NOT FFMPEG_${varname}_INCLUDE_DIRS) 
			FIND_PATH(FFMPEG_${varname}_INCLUDE_DIRS lib${shortname}/${headername} ${FFMPEG_INCLUDE_SEARCH_DIRS}) 
		ENDIF(NOT FFMPEG_${varname}_INCLUDE_DIRS) 

		FIND_LIBRARY(FFMPEG_${varname}_LIBRARIES NAMES ${shortname} PATHS ${FFMPEG_LIBRARY_SEARCH_DIRS}) 

		IF(FFMPEG_${varname}_LIBRARIES) 
			SET(FFMPEG_${varname}_FOUND 1) 
		ENDIF(FFMPEG_${varname}_LIBRARIES) 
	ENDMACRO(FFMPEG_FIND) 

	SET(FFMPEG_ROOT "$ENV{FFMPEG_DIR}" CACHE PATH "Location of FFMPEG") 

	FFMPEG_FIND(LIBAVFORMAT avformat avformat.h) 
	FFMPEG_FIND(LIBAVDEVICE avdevice avdevice.h) 
	FFMPEG_FIND(LIBAVCODEC avcodec avcodec.h) 
	FFMPEG_FIND(LIBAVUTIL avutil avutil.h) 
	FFMPEG_FIND(LIBSWSCALE swscale swscale.h)
ENDIF(NOT FFMPEG_LIBAVFORMAT_FOUND) 

SET(FFMPEG_FOUND "NO") 

IF(FFMPEG_LIBAVFORMAT_FOUND AND FFMPEG_LIBAVDEVICE_FOUND AND FFMPEG_LIBAVCODEC_FOUND AND FFMPEG_LIBAVUTIL_FOUND) 
	SET(FFMPEG_FOUND "YES") 
	SET(FFMPEG_INCLUDE_DIRS ${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS}) 
	SET(FFMPEG_LIBRARY_DIRS ${FFMPEG_LIBAVFORMAT_LIBRARY_DIRS}) 
	# libswscale optional
	
	SET(FFMPEG_LIBRARIES 
		${FFMPEG_LIBAVFORMAT_LIBRARIES} 
		${FFMPEG_LIBAVDEVICE_LIBRARIES} 
		${FFMPEG_LIBAVCODEC_LIBRARIES} 
		${FFMPEG_LIBAVUTIL_LIBRARIES})
ELSE(FFMPEG_LIBAVFORMAT_FOUND AND FFMPEG_LIBAVDEVICE_FOUND AND FFMPEG_LIBAVCODEC_FOUND AND FFMPEG_LIBAVUTIL_FOUND) 
	MESSAGE(STATUS "Could not find FFMPEG") 
ENDIF(FFMPEG_LIBAVFORMAT_FOUND AND FFMPEG_LIBAVDEVICE_FOUND AND FFMPEG_LIBAVCODEC_FOUND AND FFMPEG_LIBAVUTIL_FOUND) 
